package org.come.summonequip;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.come.entity.Goodstable;
import org.come.until.CutButtonImage;
import org.come.until.GoodsListFromServerUntil;

import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;



/**
 * 已穿装备面板
 * <p>
 * Title : JpanelNowEquip
 * </p>
 *
 * @author : HGC
 * @date : 2019年7月20日 上午11:35:14
 * @version : 1.0.0
 */
public class JpanelNowEquip extends JPanel {

    /** 已穿装备12个格子 */
    private JLabel[] equipLabs;
    /** 展示召唤兽名字 */
    private JLabel showName;
    /**
     * 滚动条下拉按钮downBtn<br>
     * 向左按钮leftBtn<br>
     * 向右按钮rightBtn<br>
     */
    private BtnSummonEquipMain downBtn, leftBtn, rightBtn;
    /** 召唤兽装备主界面 */
    private JpanelSummonEquipMain jpanelSummonEquipMain;

    /** 存放当前展示的召唤兽装备id */
    private List<BigDecimal> summonEquipList;

    /** 当前页码 */
    private int nowPage = 1;

    public JpanelNowEquip(JpanelSummonEquipMain jpanelSummonEquipMain) {
        this.jpanelSummonEquipMain = jpanelSummonEquipMain;
        String style = MyIsif.getStyle();
if ("水墨UI".equals(style)) {
        	this.setPreferredSize(new Dimension(330, 149));
        }else {
        	this.setPreferredSize(new Dimension(330, 150));
        }
        this.setOpaque(false);
        this.setLayout(null);

        /** 添加监听 */
        class MListener implements MouseListener {
            private JpanelNowEquip jpanelNowEquip;

            public MListener(JpanelNowEquip jpanelNowEquip) {
                this.jpanelNowEquip = jpanelNowEquip;
            }

            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    jpanelNowEquip.setVisible(false);
                    jpanelNowEquip.getJpanelSummonEquipMain().getScrollPane().setVisible(false);
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }

        }
        this.addMouseListener(new MListener(this));

        /** 初始化数据 */
        getSummonEquipList();
        /** 添加组件 */
        getEquipLabs();
        getDownBtn();
        getShowName();
        getRightBtn();
        getLeftBtn();
    }

    /**
     * 展示当前召唤兽装备
     */
    public void drawSummonEquips(Graphics g) {
    	String style = MyIsif.getStyle();
if ("水墨UI".equals(style)) {
    		for (int i = (nowPage - 1) * 12; i < summonEquipList.size(); i++) {
                if (GoodsListFromServerUntil.fushis.get(summonEquipList.get(i)) == null) {
                    summonEquipList.remove(i);
                    i--;
                    continue;
                }
                g.drawImage(
                        GoodsListFromServerUntil.imgpathAdaptive(
                                GoodsListFromServerUntil.fushis.get(summonEquipList.get(i)).getSkin(), 50, 50).getImage(),
                        11 + (i - (nowPage - 1) * 12) % 6 * 51, 15 + (i - (nowPage - 1) * 12) / 6 * 51, 50, 50, null);
                if (i >= 11 + (nowPage - 1) * 12) {
                    return;
                }
            }
    	}else {
    		for (int i = (nowPage - 1) * 12; i < summonEquipList.size(); i++) {
    			if (GoodsListFromServerUntil.fushis.get(summonEquipList.get(i)) == null) {
    				summonEquipList.remove(i);
    				i--;
    				continue;
    			}
    			g.drawImage(
    					GoodsListFromServerUntil.imgpathAdaptive(
    							GoodsListFromServerUntil.fushis.get(summonEquipList.get(i)).getSkin(), 50, 50).getImage(),
    					11 + (i - (nowPage - 1) * 12) % 6 * 51, 15 + (i - (nowPage - 1) * 12) / 6 * 51, 50, 50, null);
    			if (i >= 11 + (nowPage - 1) * 12) {
    				return;
    			}
    		}
    	}
    }

    /** 获取当前选中的召唤兽装备 */
    public Goodstable getSummonEquipOne(int type) {
        type += (nowPage - 1) * 12;
        for (int i = (nowPage - 1) * 12; i < summonEquipList.size(); i++) {
            if (GoodsListFromServerUntil.fushis.get(summonEquipList.get(i)) == null) {
                continue;
            }
            if (i == type) {
                return GoodsListFromServerUntil.fushis.get(summonEquipList.get(i));
            }
        }
        return null;
    }

    private ImageIcon iconBack;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        String style = MyIsif.getStyle();
if ("水墨UI".equals(style)) {
    //
        	if (iconBack == null) {
                iconBack = CutButtonImage.getImage("inkImg/newbackground/B298.png", 330, 149);
            }
            g.drawImage(iconBack.getImage(), 0, 0, 330, 149, this);
            drawSummonEquips(g);
        }else {
        	if (iconBack == null) {
        		iconBack = CutButtonImage.getImage("img/xy2uiimg/已穿装备_底板_w330,h150.png", 330, 150);
        	}
        	g.drawImage(iconBack.getImage(), 0, 0, 330, 149, this);
        	drawSummonEquips(g);
        }
    }

    public ImageIcon getIconBack() {
        return iconBack;
    }

    public void setIconBack(ImageIcon iconBack) {
        this.iconBack = iconBack;
    }

    public JLabel[] getEquipLabs() {
        if (equipLabs == null) {
            equipLabs = new JLabel[12];
            String style = MyIsif.getStyle();
if ("水墨UI".equals(style)) {
    //
            	for (int i = 0; i < equipLabs.length; i++) {
                    equipLabs[i] = new JLabel();
                    equipLabs[i].setBounds(11 + i % 6 * 51, 15 + i / 6 * 51, 50, 50);
                    // equipLabs[i].setOpaque(true);
                    equipLabs[i].addMouseListener(new MouseListenerNowEquip(i, this));
                    this.add(equipLabs[i]);
                }
            }else {
            	for (int i = 0; i < equipLabs.length; i++) {
                    equipLabs[i] = new JLabel();
                    equipLabs[i].setBounds(11 + i % 6 * 51, 15 + i / 6 * 51, 50, 50);
                    // equipLabs[i].setOpaque(true);
                    equipLabs[i].addMouseListener(new MouseListenerNowEquip(i, this));
                    this.add(equipLabs[i]);
                }
            }
        }
        return equipLabs;
    }

    public void setEquipLabs(JLabel[] equipLabs) {
        this.equipLabs = equipLabs;
    }

    public JLabel getShowName() {
        if (showName == null) {
            showName = new JLabel();
            showName.setBounds(12, 124, 144, 14);
            // showName.setOpaque(true);
            showName.setForeground(Color.white);
            showName.setFont(UIUtils.TEXT_FONT1);
            this.add(showName);
        }
        return showName;
    }

    public void setShowName(JLabel showName) {
        this.showName = showName;
    }

    public BtnSummonEquipMain getDownBtn() {
        if (downBtn == null) {
        	String style = MyIsif.getStyle();
if ("水墨UI".equals(style)) {
    //
        		downBtn = new BtnSummonEquipMain("inkImg/button/8.png", 1, "", 20, jpanelSummonEquipMain);
                downBtn.setBounds(139, 123, 18, 18);
        	}else {
        		downBtn = new BtnSummonEquipMain("img/xy2uiimg/34_png.button.xy_vscroll$down.png", 1, "", 20, jpanelSummonEquipMain);
        		downBtn.setBounds(141, 123, 19, 20);
        	}
            this.add(downBtn);
        }
        return downBtn;
    }

    public void setDownBtn(BtnSummonEquipMain downBtn) {
        this.downBtn = downBtn;
    }

    public JpanelSummonEquipMain getJpanelSummonEquipMain() {
        return jpanelSummonEquipMain;
    }

    public void setJpanelSummonEquipMain(JpanelSummonEquipMain jpanelSummonEquipMain) {
        this.jpanelSummonEquipMain = jpanelSummonEquipMain;
    }

    public List<BigDecimal> getSummonEquipList() {
        if (summonEquipList == null) {
            summonEquipList = new ArrayList<>();
        }
        return summonEquipList;
    }

    public void setSummonEquipList(List<BigDecimal> summonEquipList) {
        this.summonEquipList = summonEquipList;
    }

    public BtnSummonEquipMain getLeftBtn() {
        if (leftBtn == null) {
        	String style = MyIsif.getStyle();
if ("水墨UI".equals(style)) {
    //
        		leftBtn = new BtnSummonEquipMain("inkImg/button/10.png", 1, "", 21, this);
                leftBtn.setBounds(270, 125, 18, 18);
        	}else {
        		leftBtn = new BtnSummonEquipMain("img/xy2uiimg/29_png.button.btn_8.png", 1, "", 21, this);
        		leftBtn.setBounds(270, 125, 19, 20);
        	}
            this.add(leftBtn);
        }
        return leftBtn;
    }

    public void setLeftBtn(BtnSummonEquipMain leftBtn) {
        this.leftBtn = leftBtn;
    }

    public BtnSummonEquipMain getRightBtn() {
        if (rightBtn == null) {
        	String style = MyIsif.getStyle();
if ("水墨UI".equals(style)) {
    //
        		rightBtn = new BtnSummonEquipMain("inkImg/button/9.png", 1, "", 22, this);
                rightBtn.setBounds(270 + 25, 125, 18, 18);
        	}else {
        		rightBtn = new BtnSummonEquipMain("img/xy2uiimg/36_png.button.btn_7.png", 1, "", 22, this);
        		rightBtn.setBounds(270 + 25, 125, 18, 18);
        	}
            this.add(rightBtn);
        }
        return rightBtn;
    }

    public void setRightBtn(BtnSummonEquipMain rightBtn) {
        this.rightBtn = rightBtn;
    }

    public int getNowPage() {
        return nowPage;
    }

    public void setNowPage(int nowPage) {
        this.nowPage = nowPage;
    }

}
